package com.cakeclient;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.cakeclient.Model.Basket;
import com.cakeclient.Model.ProductType;
import com.cakeclient.Model.Store;

public class ClientProductCardFragment extends SherlockFragment {

    private ProductType product;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.client_product_card,container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        if (product != null){
            TextView titleTextView = (TextView) getView().findViewById(R.id.client_products_card_title_textview);
            TextView descriptionTextView = (TextView) getView().findViewById(R.id.client_product_card_description_textview);
            TextView priceTextView = (TextView) getView().findViewById(R.id.client_product_card_price_textview);
            titleTextView.setText(product.getName());
            descriptionTextView.setText(product.getDescription());
            priceTextView.setText(product.getLocalPrice(Store.getLocalStore(getActivity().getBaseContext())).toString() + " €");

        }

        // Ajout du listener pour ajouter un produit au panier
        Button buttonAdd = (Button) getSherlockActivity().findViewById(R.id.button_add);
        buttonAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Basket basket = ((MainActivity) getSherlockActivity()).basket;
                basket.getProducts().add(product);
                Toast toast = Toast.makeText(getSherlockActivity(), "Product added", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }

    public ProductType getProduct() {
        return product;
    }

    public void setProduct(ProductType product) {
        this.product = product;
    }


}
